package com.icesoft.system.service.impl;

import com.icesoft.framework.core.exception.BusinessException;
import com.icesoft.framework.core.service.impl.BaseServiceImpl;
import com.icesoft.system.entity.Company;
import com.icesoft.system.handler.CompanyUpdateHandler;
import com.icesoft.system.mapper.CompanyMapper;
import com.icesoft.system.service.CompanyService;
import com.icesoft.system.vo.CompanyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * appuserService业务层处理
 *
 * @author madiao
 * @date 230212
 */
@Slf4j
@Service
public class CompanyServiceImpl extends BaseServiceImpl<CompanyMapper, Company> implements CompanyService {

	@Autowired(required = false)
	private List<CompanyUpdateHandler> companyUpdateHandlerList;

	@Override
	public boolean createEntity(Company entity) {
		boolean isOK = super.createEntity(entity);
		if (companyUpdateHandlerList != null) {
			for (CompanyUpdateHandler companyUpdateHandler : companyUpdateHandlerList) {
				companyUpdateHandler.createCompany(entity);
			}
		}
		return isOK;
	}

	@Override
	public boolean updateEntity(Company entity) {
		boolean isOK = super.updateEntity(entity);
		if (companyUpdateHandlerList != null) {
			for (CompanyUpdateHandler companyUpdateHandler : companyUpdateHandlerList) {
				companyUpdateHandler.updateCompany(entity);
			}
		}
		return isOK;
	}

	@Transactional
	@Override
	public boolean deleteEntity(Serializable id) {
		boolean isOK = super.deleteEntity(id);
		if (companyUpdateHandlerList != null) {
			for (CompanyUpdateHandler companyUpdateHandler : companyUpdateHandlerList) {
				companyUpdateHandler.deleteCompany(id);
			}
		}
		return isOK;
	}

	public CompanyVO findByAppletAppid(String appId) {
		Company company = lambdaQuery().eq(Company::getAppletAppid, appId).one();
		if (company == null) {
			throw new NullPointerException("未找到公司！");
		}
		return getViewObject(company, CompanyVO.class);
	}
}
